<?php
namespace common\components\helpers;

use yii\base\BaseObject;
use yii\helpers\ArrayHelper;

class EventData extends BaseObject{

    private $_config;

    private $_configType;

    private $_configSendGroup;

    public $className;

    public $classOwner;

    public $eventName;

    public $handler;

    public function init(){
        $this->_config=\Yii::$app->params['event'];
        $this->_configType=\Yii::$app->params['type'];
        $this->_configSendGroup=\Yii::$app->params['send_group'];
        parent::init();
    }

    public function getEventArray()
    {
        $cache = $this->_config;
        $eventarray = [];
        foreach ($cache as $k => $v) {
            if ($v['model'] == $this->classOwner && $v['behavior']== $this->className){
                if(!empty($v['event'])&&!empty($v['handler'])) {
                    $eventarray[$v['event']] = $v['handler'];
                }
            }
        }
        return $eventarray;
    }

    public function getBehaviorArray()
    {
        $behaviers_cache = $this->_config;
        $behaviorarray = [];
        foreach ($behaviers_cache as $k => $v) {
            if ($v['model'] == $this->className) {
                $class['class'] = $v['behavior'];
                $behaviorarray[] = isset($v['config'])&&is_array($v['config'])?array_merge($class, $v['config']):$class;
            }
        }
        return $behaviorarray;
    }

    public function getEventKey()
    {
        $cache = $this->_config;
        foreach ($cache as $k => $v) {
            if ($v['model'] == $this->classOwner && $v['behavior']== $this->className && $v['event'] == $this->eventName && $v['handler'] == $this->handler){
                return $k;
            }
        }
        return 0;
    }

    public function getNameByKey($key){
        return isset($this->_config[$key]['name'])?$this->_config[$key]['name']:'不存在';
    }
    public function getTypeByKey($key){
        return isset($this->_configType[$key]['name'])?$this->_configType[$key]['name']:'不存在';
    }
    public function getSendGroupByKey($key){
        return isset($this->_configSendGroup[$key]['name'])?$this->_configSendGroup[$key]['name']:'不存在';
    }
    public function getSendGroupArrayByKey($key){
        return isset($this->_configSendGroup[$key])?$this->_configSendGroup[$key]:'';
    }
    public function getTypeList(){
        return ArrayHelper::getColumn($this->_configType,'name');
    }
    public function getSendGroupList(){
        return ArrayHelper::getColumn($this->_configSendGroup,'name');
    }
    public function getNameList(){
        return ArrayHelper::getColumn($this->_config,'name');
    }
}